package com.uwo.house.controller;

import com.uwo.house.beans.request.VisitOrderReq;
import com.uwo.house.beans.response.VisitOrderResp;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.ICsmVisitService;
import com.uwo.house.util.DateUtils;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 预约参观样板房
 * Created by YZYG on 2017/7/14.
 */
@Controller
@RequestMapping("/visit")
public class CsmVisitController {
    @Autowired
    public ICsmVisitService csmVisitService;

    /**
     * 用户预约参观
     *
     * @param visitOrderReq
     * @return
     */
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public Map orderVisit(@RequestBody VisitOrderReq visitOrderReq,
                          @RequestHeader(value = "Authorization") String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        visitOrderReq.setOrderId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        String userId = jwtToken.getUser().getId();
        visitOrderReq.setUserId(userId);
        int result = this.csmVisitService.orderVisit(visitOrderReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "预约成功");
        }
        return UtilReturn.resErrorMap("预约失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 管理后台-条件查询预约参观信息
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getVisitOrderList(@RequestParam(value = "userId", required = false) String userId,
                                 @RequestParam(value = "dateFrom", required = false) String dateFrom,
                                 @RequestParam(value = "dateTo", required = false) String dateTo) {
        List<VisitOrderResp> list = this.csmVisitService.getVisitOrderList(userId, DateUtils.getDate(dateFrom), DateUtils.getDate(dateTo));
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     *删除用户参观预约
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteVisitOrder(@RequestParam("id") String id) {
        int result = this.csmVisitService.deleteVisitOrder(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败",UtilReturn.ERROR_CODE);
    }

    /**
     * 根据选择样板房获得地址
     *
     * @param houseId
     * @return
     */
    @RequestMapping(value = "/address", method = RequestMethod.GET)
    @ResponseBody
    public Map getSampleHouseAddress(@RequestParam("houseId") int houseId) {
        String address = this.csmVisitService.getSampleHouseAddress(houseId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, address);
    }
}
